package config

import (
	"github.com/joho/godotenv"
	qiniuStorage "github.com/qiniu/go-sdk/v7/storage"
	"os"
)

type Config struct {
	Server struct {
		Address string
	}
	MySQL struct {
		Host     string
		Port     string
		Username string
		Password string
		Database string
	}
	Redis struct {
		Address  string
		Password string
		DB       int
	}
	Upload struct {
		UploadPath     string
		QiniuAccessKey string
		QiniuSecretKey string
		QiniuBucket    string
		QiniuUpToken   string
		QiniuMac       interface{}
		QiniuZone      qiniuStorage.Zone
	}
}

var Cfg *Config

func Init() *Config {
	// 加载.env文件
	godotenv.Load()

	Cfg = &Config{}

	// 服务器配置
	Cfg.Server.Address = getEnv("SERVER_ADDRESS", ":9000")
	// MySQL配置
	Cfg.MySQL.Host = getEnv("MYSQL_HOST", "103.197.26.92")
	Cfg.MySQL.Port = getEnv("MYSQL_PORT", "3306")
	Cfg.MySQL.Username = getEnv("MYSQL_USER", "root")
	Cfg.MySQL.Password = getEnv("MYSQL_PASSWORD", "rootpassword")
	Cfg.MySQL.Database = getEnv("MYSQL_DATABASE", "cat-collage-backend")

	// 在Init函数中初始化Redis配置
	Cfg.Redis.Address = getEnv("REDIS_ADDRESS", "103.197.26.92:6379")
	Cfg.Redis.Password = getEnv("REDIS_PASSWORD", "1225")
	Cfg.Redis.DB = 0 // 使用默认的DB
	// 在Init函数中初始化七牛云配置
	Cfg.Upload.UploadPath = getEnv("UPLOAD_PATH", "http://qianduanym.top/cat-collage-storage/images")
	Cfg.Upload.QiniuSecretKey = getEnv("QINIU_SECRET_KEY", "02ljmpxq-MOqX5RGg6dmeDa9iFC55eMe5kVfMzUI")
	Cfg.Upload.QiniuAccessKey = getEnv("QINIU_ACCESS_KEY", "-r4K43awFJgf9NRVp3x-xu9UHyQ1mh4IZofviHGg")
	Cfg.Upload.QiniuBucket = getEnv("QINIU_BUCKET", "cat-collage-storage")
	return Cfg
}

// Helper function to get environment variables with a default
func getEnv(key, defaultValue string) string {
	value, exists := os.LookupEnv(key)
	if !exists {
		value = defaultValue
	}

	return value
}
